package org.sith.gl.taskmanager.client.tasks;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.sith.gl.taskmanager.client.AbstractVerticalPane;
import org.sith.gl.taskmanager.client.TasksRemoteService;
import org.sith.gl.taskmanager.client.beans.TaskEntityGWT;
import org.sith.gl.taskmanager.client.beans.UserEntityGWT;

import java.util.ArrayList;
import java.util.List;


public class TasksPane extends AbstractVerticalPane {


    @Override
    protected void initData() {
        TasksRemoteService.App.getInstance().getTasksForCurrentUser(new AsyncCallback<UserEntityGWT>() {
            @Override
            public void onFailure(Throwable throwable) {
                Window.alert("Error on receive tasks for user");
            }

            @Override
            public void onSuccess(UserEntityGWT userEntityGWT) {
                drawPane(userEntityGWT);
            }
        });
    }

    private void drawPane(final UserEntityGWT userEntityGWT) {
        this.clear();
        setWidth("100%");
        setHeight("100%");
        add(new Label("Tasks for user: " + userEntityGWT.getLogin()));
        HorizontalPanel buttons = new HorizontalPanel();
        Button addTask = new Button("Add task");
        addTask.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                AddTaskPopup.show(userEntityGWT);
            }
        });
        buttons.add(addTask);
        buttons.add(refreshButton);
        add(buttons);

        ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setSize("100%", "400px");

        VerticalPanel tasksPanel = new VerticalPanel();
        tasksPanel.setWidth("100%");
        scrollPanel.add(tasksPanel);
        DisclosurePanel openTasksPanel = getTaskPanel("Open tasks", true, getTargetTasks(userEntityGWT.getAssignedEntities(), false));
        tasksPanel.add(openTasksPanel);
        DisclosurePanel closedTaskPanel = getTaskPanel("Closed tasks", false, getTargetTasks(userEntityGWT.getAssignedEntities(), true));
        tasksPanel.add(closedTaskPanel);
        DisclosurePanel myTaskPanel = getTaskPanel("Tasks written by me", false, userEntityGWT.getWritedEntites());
        tasksPanel.add(myTaskPanel);
        add(scrollPanel);

    }

    private List<TaskEntityGWT> getTargetTasks(List<TaskEntityGWT> assignedEntities, boolean isClosed) {
        List<TaskEntityGWT> result = new ArrayList<TaskEntityGWT>();
        for (TaskEntityGWT taskEntityGWT : assignedEntities) {
            if (taskEntityGWT.isComplete() == isClosed) {
                result.add(taskEntityGWT);
            }
        }
        return result;
    }

    private DisclosurePanel getTaskPanel(String headerText, boolean isPanelOpen, List<TaskEntityGWT> tasks) {
        DisclosurePanel disclosurePanel = new DisclosurePanel(headerText);
        disclosurePanel.setOpen(isPanelOpen);
        disclosurePanel.setWidth("100%");
        VerticalPanel content = new VerticalPanel();
        content.setWidth("100%");
        disclosurePanel.add(content);
        for (TaskEntityGWT taskEntityGWT : tasks) {
            TaskPanel panel = new TaskPanel(taskEntityGWT);
            panel.createWidget();
            content.add(panel);
        }
        return disclosurePanel;
    }

    @Override
    protected String getPagePosition() {
        return "tasks";
    }
}
